package no.monsen.commons.factory;

import java.net.URL;
import java.util.Properties;

import no.monsen.commons.exception.MonsenException;

/**
 * Property factory used to easy change labels in the client application
 * 
 * @author Marius Breivik
 * 
 */
public class MonsenPropertiesFactory {

	private Properties properties;
	private static MonsenPropertiesFactory me = null;
	private final static String PROPERTY_FILE = "/monsen.properties";

	/**
	 * Private constructor. The property file is also loaded from here.
	 */
	private MonsenPropertiesFactory() {
		loadPropertieFile();
	}

	/**
	 * Factory method for MonsenPropertiesFactory class.
	 * 
	 * @return An instance of MonsenPropertyFactory
	 */
	public static MonsenPropertiesFactory getInstance() {
		// Check if me (instance of this class) is null)
		if (me == null) {
			me = new MonsenPropertiesFactory();
		}
		return me;
	}

	public String getProperty(String key) {
		if (!properties.isEmpty()) {
			return properties.getProperty(key);
		}
		// We will hopefully not end here...:-)
		return null;
	}

	/**
	 * This method loads the property file defined.
	 * 
	 * @throws MonsenException
	 */
	private void loadPropertieFile() {
		properties = new Properties();
		try {
			URL prop = this.getClass().getResource(PROPERTY_FILE);
			properties.load(prop.openStream());

		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
